package com.ruoyi.xiaoshouchuku.service.impl;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.util.*;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.shangpinxinxi.domain.Shangpinxinxi;
import com.ruoyi.shangpinxinxi.domain.Shangpinxinxi2;
import com.ruoyi.xiaoshouchuku.domain.TestResult;
import com.ruoyi.xiaoshouchuku.domain.XiaoshouChuku2;
import com.ruoyi.xiaoshouchuku.domain.XiaoshouVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.xiaoshouchuku.mapper.XiaoshouChukuMapper;
import com.ruoyi.xiaoshouchuku.domain.XiaoshouChuku;
import com.ruoyi.xiaoshouchuku.service.IXiaoshouChukuService;
import com.ruoyi.common.core.text.Convert;

/**
 * 销售出库Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-01-10
 */
@Service
public class XiaoshouChukuServiceImpl implements IXiaoshouChukuService {
    @Autowired
    private XiaoshouChukuMapper xiaoshouChukuMapper;

    /**
     * 查询销售出库
     *
     * @param id 销售出库主键
     * @return 销售出库
     */
    @Override
    public XiaoshouChuku selectXiaoshouChukuById(Long id) {
        return xiaoshouChukuMapper.selectXiaoshouChukuById(id);
    }

    /**
     * 查询销售出库金额
     *
     * @param chukuCangku 销售出库主键
     * @return 销售出库
     */
    @Override
    public Double selectXiaoshouChukuByjiner(String chukuCangku, String today) {
        String[] date_ = today.split("-");
        String firstday = null,lastday = null;
        switch (Integer.parseInt(date_[1])){
            case 10:
                firstday=date_[0]+"-10-1";lastday=date_[0]+"-10-31";
                break;
            case 8:
                firstday=date_[0]+"-8-1";lastday=date_[0]+"-8-31";
                break;
            case 7:
                firstday=date_[0]+"-7-1";lastday=date_[0]+"-7-31";
                break;
            case 5:
                firstday=date_[0]+"-5-1";lastday=date_[0]+"-5-31";
                break;
            case 3:
                firstday=date_[0]+"-3-1";lastday=date_[0]+"-3-31";
                break;
            case 1:
                firstday=date_[0]+"-1-1";lastday=date_[0]+"-1-31";
                break;
            case 12:
                firstday=date_[0]+"-12-1";lastday=date_[0]+"-12-31";
                break;

            case 4:
                firstday=date_[0]+"-4-1";lastday=date_[0]+"-4-30";
                break;
            case 6:
                firstday=date_[0]+"-6-1";lastday=date_[0]+"-6-30";
                break;
            case 9:
                firstday=date_[0]+"-9-1";lastday=date_[0]+"-9-30";
                break;
            case 11:
                firstday=date_[0]+"-11-1";lastday=date_[0]+"-11-30";
                break;

            case 2:
                firstday=date_[0]+"-2-1";lastday=date_[0]+"-2-29";
                break;
            default:break;
        }
        Double l = xiaoshouChukuMapper.selectXiaoshouChukuByjiner(chukuCangku, firstday,lastday);
        return l;
    }

    /**
     * 查询销售出库数量
     *
     * @param chukuCangku 销售出库主键
     * @return 销售出库
     */
    @Override
    public Long selectXiaoshouChukuByshuliang(String chukuCangku, String today) {
        return xiaoshouChukuMapper.selectXiaoshouChukuByCangKu(chukuCangku);
    }


    /**
     * 查询销售出库列表
     *
     * @param xiaoshouChuku 销售出库
     * @return 销售出库
     */
    @Override
    public List<XiaoshouChuku> selectXiaoshouChukuList(XiaoshouChuku xiaoshouChuku) {
        return xiaoshouChukuMapper.selectXiaoshouChukuList(xiaoshouChuku);
    }

    /**
     * 新增销售出库
     *
     * @param xiaoshouChuku 销售出库
     * @return 结果
     */
    @Override
    public int insertXiaoshouChuku(XiaoshouChuku xiaoshouChuku) {
        return xiaoshouChukuMapper.insertXiaoshouChuku(xiaoshouChuku);
    }


    /**
     * 修改销售出库
     *
     * @param xiaoshouChuku 销售出库
     * @return 结果
     */
    @Override
    public int updateXiaoshouChuku(XiaoshouChuku xiaoshouChuku) {
        return xiaoshouChukuMapper.updateXiaoshouChuku(xiaoshouChuku);
    }

    /**
     * 批量删除销售出库
     *
     * @param ids 需要删除的销售出库主键
     * @return 结果
     */
    @Override
    public int deleteXiaoshouChukuByIds(String ids) {
        return xiaoshouChukuMapper.deleteXiaoshouChukuByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除销售出库信息
     *
     * @param id 销售出库主键
     * @return 结果
     */
    @Override
    public int deleteXiaoshouChukuById(Long id) {
        return xiaoshouChukuMapper.deleteXiaoshouChukuById(id);
    }

    @Override
    public XiaoshouChuku selectXiaoshouChukuBychukuDanhao(String chukuDanhao) {
        return xiaoshouChukuMapper.selectXiaoshouChukuBychukuDanhao(chukuDanhao);
    }

    @Override
    public String selectXiaoshouChukuIdByLastOne() {
        return xiaoshouChukuMapper.selectXiaoshouChukuIdByLastOne();
    }

    @Override
    public List<TestResult> selectXiaoshouChukupaihangByShuliang(String today, String cangku) {

        List<TestResult> list = xiaoshouChukuMapper.selectXiaoshouChukupaihangByShuliang(cangku, today);
        return list;
    }

    @Override
    public List<String> selectXiaoshouChukuByDate(String cangku, String start, String endDay) {
        Date startDate = null;
        Date endDate = null;
        try {
            startDate = new Date(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(start).getTime());
            endDate = new Date(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(endDay).getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }

        List<String> xlist = new ArrayList<>();
        String jiner, cangkuName;
        int index = 1;
        if (cangku != null) {
            List<XiaoshouVo> list = xiaoshouChukuMapper.selectXiaoshouChukuByDate1(startDate, endDate);
            for (XiaoshouVo xiaoshouVo : list) {
                cangkuName = xiaoshouVo.getCangkuName();
                if (cangku.equals(cangkuName) && index <= 10) {
                    index += 1;
                    jiner = xiaoshouVo.getTotaljiner().toString();
                    LocalDateTime date = xiaoshouVo.getDate();
                    String month = date.getMonthValue() + "月";
                    String day = date.getDayOfMonth() + "日";
                    String date1 = month + day;
                    String result = date1 + "@" + cangkuName + "@" + jiner;
                    xlist.add(result);
                }

            }
        } else {
            List<XiaoshouVo> list = xiaoshouChukuMapper.selectXiaoshouChukuByDate(startDate, endDate);
            for (XiaoshouVo xiaoshouVo : list) {
                cangkuName = xiaoshouVo.getCangkuName();
                jiner = xiaoshouVo.getTotaljiner().toString();
                LocalDateTime date = xiaoshouVo.getDate();
                String month = date.getMonthValue() + "月";
                String day = date.getDayOfMonth() + "日";
                String date1 = month + day;
                String result = date1 + "@" + cangkuName + "@" + jiner;
                xlist.add(result);
            }
        }
        System.out.println(xlist);
        return xlist;
    }

    @Override
    public List<XiaoshouChuku2> selectXiaoshouChukuListAndQuanXian(XiaoshouChuku xiaoshouChuku) {
        List<XiaoshouChuku> shangpinxinxiList = xiaoshouChukuMapper.selectXiaoshouChukuList(xiaoshouChuku);
        List<XiaoshouChuku2> shangpinxinxi2List = new ArrayList<>();
        for (XiaoshouChuku s: shangpinxinxiList
        ) {
            XiaoshouChuku2 xiaoshouChuku2 = new XiaoshouChuku2();
            xiaoshouChuku2.setId(s.getId());
            xiaoshouChuku2.setChukuDanhao(s.getChukuDanhao());
            xiaoshouChuku2.setChukuDate(s.getChukuDate());
            xiaoshouChuku2.setJiner(s.getJiner());
            xiaoshouChuku2.setPaymentMethod(s.getPaymentMethod());
            xiaoshouChuku2.setChukuJingshouren(s.getChukuJingshouren());
            xiaoshouChuku2.setChukuCangku(s.getChukuCangku());
            xiaoshouChuku2.setGoodId(s.getGoodId());
            xiaoshouChuku2.setShangpinLeibie(s.getShangpinLeibie());
            xiaoshouChuku2.setGoodName(s.getGoodName());
            xiaoshouChuku2.setGoodGuige(s.getGoodGuige());
            xiaoshouChuku2.setShuliang(s.getShuliang());
            xiaoshouChuku2.setChukuDanjia(s.getChukuDanjia());
            xiaoshouChuku2.setBeizhu(s.getBeizhu());
            shangpinxinxi2List.add(xiaoshouChuku2);
        }
        return shangpinxinxi2List;
    }
}
